<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>菜单管理</title>
    <link rel="stylesheet" type="text/css" href="../lib/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../lib/themes/icon.css">
	<script type="text/javascript" src="../lib/jquery.min.js"></script>
	<script type="text/javascript" src="../lib/jquery.easyui.min.js"></script>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <script type="text/javascript" src="../lib/jiekou.js"></script>
    <script type="text/javascript" src="../lib/tools.js"></script>
    <link href="../lib/mask/jquery.loadmask.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../lib/mask/jquery.loadmask.min.js"></script>
</head>
<body>
    <div style="padding:5px;background:#fafafa;width:500px;">
        <a href="#" onclick="menu.openwin()" class="easyui-linkbutton" iconCls="icon-add">添加菜单</a>
        <a href="#" onclick="menu.delete()" class="easyui-linkbutton" iconCls="icon-remove">删除菜单</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="menu.updatewin()">修改菜单</a>
        <text style="color:rgb(251, 7, 88);font-size: 2px;">*同级菜单排列值为数字,大着排前面*</text>
    </div>
    <hr>
    <div  style="padding:5px">
        <ul class="easyui-tree" id="tree_menu"></ul>
    </div>
    <div style="margin:20px 0;">
		
	</div>
  
    <div id="win" class="easyui-window" title="菜单操作" closed="true" style="padding:30px 60px;width:600px;height:500px;">

        <form id="ff" method="post">
            <p style="color:rgb(251, 7, 88);">*添加菜单,排列顺序为数字,越大越排前面*</p>
            <input  id="hiddeninput" type="hidden"  name="id" iddata="" piddata="" />
			<div  style="margin-bottom:20px;display: block;" id="divparentName">
				<input class="easyui-textbox" name="parentName" id="parentName" editable=false style="width:100%" data-options="label:'父目录:'">
			</div>

            <div style="margin-bottom:20px;display: block;" id="divisdir">
				<select id="isdir" class="easyui-combobox" name="isdir" label="创建方式" style="width:100%">
                    <option value="">请选择</option>
                    <option value="1">创建菜单目录</option>
                    <option value="0">创建菜单</option>
				</select>
			</div>
			<div style="margin-bottom:20px">
				<input class="easyui-textbox" name="menuname" id="menuname" style="width:100%" data-options="label:'菜单名字:'">
			</div>
			<div  style="margin-bottom:20px;display: block;"  id="divpath">
				<input class="easyui-textbox" name="path" id="path" style="width:100%" data-options="label:'跳转路径:'">
			</div>
			<div  style="margin-bottom:20px">
				<input class="easyui-textbox" name="ordenum" id="ordenum" style="width:100%" data-options="label:'排列顺序:'">
			</div>
	
		<div style="text-align:center;padding:5px 0;margin-top: 10px; margin-top: 0 auto;">
			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="menu.add()" id="menusubmit" style="width:80px">提交</a>
		
		</div>
	</form>
     
    </div>

    
    <script>

        var menu={
            openwin:function(){
                var node = $('#tree_menu').tree('getSelected');
                    if(node==null || node.isdir==0){
                        $.messager.alert("提示","请点击选择一个菜单目录");
                        return;
                    }
                    //先清理，在赋值
                   this.clear();
                   $('#menusubmit').linkbutton({
                            text: '新增'
                    });
                    
                    $("#parentName").textbox("setValue",node.text);
                    $("#hiddeninput").attr("piddata",node.id)
                  
                    $('#win').window('open');
                    $("#isdir").combobox({onSelect:function(param){
                    
                            if(param.value=='1'){
                                //创建目录
                                $("#divpath").css({"display":"none"});
                            }else if(param.value == '0'){
                               //创建菜单
                               $("#divpath").css({"display":"block"});
                            }else{
                                $("#divpath").css({"display":"block"});
                            }
                    }});
                 
                    //
            },
            add:function(){
                //
                var isdir=$("#isdir").combobox('getValue');
                var id =$("#hiddeninput").attr("iddata");
                var pid=$("#hiddeninput").attr("piddata");
                var parentName=$("#parentName").textbox("getValue");
                var menuname=$("#menuname").textbox("getValue");
                var path=$("#path").textbox("getValue");
                var ordenum=$("#ordenum").textbox("getValue");
                console.log("isdir",isdir,"id",id,"parentName",parentName,"pid",pid);
                console.log("menuname",menuname,"path",path,"ordernum",ordenum);

                if(id && id>0){
                 //更新操作
                 if(isdir=="0"){
                    if (path==""){
                        $.messager.alert("update参数错误","path跳转路径空");
                        return;
                    }
                 }
                 if (ordenum==""  || menuname=="" ){
                        $.messager.alert("update参数错误","排列顺序,菜单名为空");
                        return;
                    }
                 //end
                }else{
                    //start
                        if(isdir==""){
                            $.messager.alert("insert参数错误","请选择创建方式");
                            return;
                        }
                        if (ordenum==""  || menuname==""){
                            $.messager.alert("insert参数错误","排列顺序,菜单名为空");
                            return;
                        }

                        if(isdir == "0"){ //创建菜单
                            if(path==""){
                                $.messager.alert("insert参数错误","创建菜单而path为空");
                                return;
                            }
                        }
                    //end
                }
                $("#win").mask("数据加载中...");
               
                var menudata={
                        ordernum:ordenum+"",
                        path:path+"",
                        menuname:menuname+"",
                        isdir:isdir+"",
                        pid:pid+"",
                        id:id+""
                            };
                var addmenuSuccess =function(response){
                    $("#win").unmask();                 
                    if(response.data.code ==200){
                        $('#tree_menu').tree('reload');
                        $('#win').window('close');
                    }else{
                        Network.alert(response.data.msg);
                    }

                };
                var addmenuError=function(error){
                    $("#win").unmask();
                     $('#win').window('close');
                     console.log("===>",error);
                    Network.alert("addmenu交易报错");
                }

                Network.apost(jiekou.addMenu,menudata,addmenuSuccess,addmenuError);
                // axios.post(liuhangkai.geturl("/addmenu"), {
                //         ordernum:ordenum+"",
                //         path:path+"",
                //         menuname:menuname+"",
                //         isdir:isdir+"",
                //         pid:pid+"",
                //         id:id+""
                //             }).then(function (response) {
                //                    $("#win").unmask();
                                    
                //                     if(response.data.code ==200){
                //                         $('#tree_menu').tree('reload');
                //                        $('#win').window('close');
                //                     }else{
                //                         $.messager.alert("添加失败",response.data.msg);
                //                     }}).catch(function (error) {
                //                         $("#win").unmask();
                //                     $('#win').window('close');
                //                     $.messager.alert("交易报错",error);
                //               });

                              //end

            },
            updatewin:function(){
                var node = $('#tree_menu').tree('getSelected');
                if(node==null||node.id ==0){
                    $.messager.alert("提示","请点击选择,请不要选择根");
                    return;
                };
                // var node1 = $('#tree_menu').tree('getParent', node.target);
                // console.log("node",node);
                // console.log("==par",node1);
                // var node2 = $('#tree_menu').tree('getParent', node1.target);
                // console.log("==par",node2);
                
                // return;
                this.clear();
                //然后在赋值操作
                if (node){
                console.log(node);
                $("#isdir").combobox('setValue', node.isdir);
                $("#hiddeninput").attr("iddata",node.id);
                $("#menuname").textbox("setValue",node.text);
                $("#path").textbox("setValue",node.path);
                $("#ordenum").textbox("setValue",node.sortnum);
                $('#win').window('open');
                $('#menusubmit').linkbutton({
                            text: '修改'
                    });
                $("#divisdir").css({"display":"none"});
                $("#divparentName").css({"display":"none"});
                
                if(node.isdir == "1"){
                    $("#divpath").css({"display":"none"});
                }else{
                    $("#divpath").css({"display":"block"});
                }
            }


            },
            delete:function(){
                var node = $('#tree_menu').tree('getSelected');
                    if (!node){
                        $.messager.alert('提示','请选择菜单');
                        return;
                    }

                    if(node.id=="0" ||node.id=="1"|| node.pid =="1" || node.pid == "32"){
                        $.messager.alert('提示','系统目录或者业务调度功能不能删除');
                        return;
                    }

                    $.messager.confirm('确认', "确定要删除-"+node.text+"-吗？", function(r){	
								if(r){
                                    //begin
                                    $("#win").mask("数据加载中...");
                                    $.ajax({
                                            url: Network.geturl("/v1/deletemenu"),
                                            type:'post',
                                            data:{
                                                id:node.id,
                                                pid:node.pid,
                                                isdir:node.isdir
                                            },
                                            error:function(){
                                                $("#win").unmask();
                                                $.messager.alert('提示','请求失败!')
                                            },
                                            success:function(res){
                                                $("#win").unmask();
                                                console.log(res);
                                                if(res.code=='200'){
                                                    $('#tree_menu').tree('reload');
                                                }else{

                                                    $.messager.alert('提示',res.msg) 

                                                }
                                            }
                                    });
                                    //end
                                }
					 });

                
                //end
            },
            clear:function(){
                $("#isdir").combobox('setValue', '');
                $("#hiddeninput").attr("iddata","");
                $("#hiddeninput").attr("piddata","");
                $("#parentName").textbox("setValue","");
                $("#menuname").textbox("setValue","");
                $("#path").textbox("setValue","");
                $("#ordenum").textbox("setValue","");
                //三个空间默认是显示
                $("#divpath").css({"display":"block"});
                $("#divisdir").css({"display":"block"});
                $("#divparentName").css({"display":"block"});
            }
        };

        
        $(function(){

              
                //加载全部节点
                $("#tree_menu").tree({
                     url: Network.geturl("/v1/d"),
                     animate:true,
                     lines:true,
                     onClick: function (node) {
                           
                     },formatter:function(node){
                        // return node.text+"----排列顺序-"+node.sortnum;
                        var v=node.sortnum;
                        var des="目录";
                        if(!v){
                            v=0;
                        }
                        if(node.isdir =="0"){
                            des="子菜单";
                        }
                        return node.text+'<text style="color:red; font-size:2px;">'+'&nbsp&nbsp&nbsp&nbsp(sort-'+v+'&nbsp&nbsp'+'id-'+node.id+'&nbsp&nbsp'+des+')</text>';
                    },
                     onLoadError:function(err){
                          $.messager.alert('tree','网络故障');
                    }
                });

            });

         
        
        
     </script>
</body>
</html>